#include <iostream>

#include <omp.h>

#include "ogsolverBBP.h"

/**
 * A BBP solver : starts with jobs with last due dates
 *
 */
OGSolverBBP::OGSolverBBP(GSupplyLinkProblem& pb):problem(pb),
    bestSolution(pb),bestSolutionDueDateOrdered(pb),currentSolutionDueDateOrdered(pb) {
}

OGSolverBBP::~OGSolverBBP() {
//    if (this->bestSolution != NULL ) delete this->bestSolution ;
//    if (this->bestSolutionDueDateOrdered != NULL ) delete this->bestSolutionDueDateOrdered ;
//    if (this->currentSolutionDueDateOrdered != NULL ) delete this->currentSolutionDueDateOrdered ;
}

void OGSolverBBP::initBestSolutionWithFFD() {
    int nbrbatch=0 ;
    vector<double> tabRemainingCapacity ;

    for (int job=problem.getNP()-1;job>=0;job--) {
        // Find a suitable batch
        int batchjob = nbrbatch-1 ;
        bool found = false ;
        double jobsize = problem.getJobSize(job) ;
        while (!found && batchjob>=0) {
            if (tabRemainingCapacity[batchjob]>=jobsize) {
                found = true ;
            }
            else
                batchjob-- ;
        }

        if (!found) { // Create a new batch
            tabRemainingCapacity.insert(tabRemainingCapacity.begin(),problem.getTransporter(0).getCapacity()) ;
            for (int job2=job+1;job2<problem.getNP();++job2)
                bestSolutionDueDateOrdered.setBatchIndice(job2, bestSolutionDueDateOrdered.getBatchIndice(job2)+1) ;
            batchjob = 0 ;
            nbrbatch++ ;
        }
        bestSolutionDueDateOrdered.setBatchIndice(job, batchjob+1) ;
        bestSolutionDueDateOrdered.setDeliveryCompletionTime(job, (double)batchjob+1) ;
        tabRemainingCapacity[batchjob] -= jobsize ;
    }

    bestSolutionDueDateOrdered.evaluate() ;
    cout << "FFD solution = " << bestSolutionDueDateOrdered << endl ;
}

/**
 *
 */
void OGSolverBBP::solve() {
    solveInit() ;
    solveMain() ;
    solveEnd() ;
}


